fix: duplicate ordering state field names in partial aggregates - #25196
fix: duplicate ordering state field names in partial aggregates#25196cdelmonte-zg wants to merge 3 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #25196 +/- ##
==========================================
- Coverage 81.89% 81.88% -0.01%
==========================================
Files 1133 1133
Lines 424660 424718 +58
Branches 424660 424718 +58
==========================================
+ Hits 347765 347798 +33
- Misses 56283 56306 +23
- Partials 20612 20614 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jayzhan211
left a comment
There was a problem hiding this comment.
Thanks @cdelmonte-zg, all suggestions are non-blocking
Maybe we could also add the change to docs/source/library-user-guide/upgrading/56.0.0.md
| .into(), | ||
| ]; | ||
| fields.extend(args.ordering_fields.iter().cloned()); | ||
| fields.extend(args.ordering_fields.iter().enumerate().map(|(idx, field)| { |
There was a problem hiding this comment.
could we add helper to reduce duplication
/// Namespaces ordering fields so state field names stay unique per query.
pub fn ordering_state_fields(name: &str, ordering_fields: &[FieldRef]) -> Vec<FieldRef> {
ordering_fields
.iter()
.enumerate()
.map(|(idx, f)| {
Arc::new(f.as_ref().clone().with_name(format_state_name(
name,
&format!("ordering_{idx}_{}", f.name()),
)))
})
.collect()
}There was a problem hiding this comment.
I think the first idx might be enough 🤔
first_value(value)[ordering_0_timestamp@0] becomes first_value(value)[ordering_0]
There was a problem hiding this comment.
Moved the logic into an helper. As regards the naming scheme, I think you are right. I kept the field name mainly to make debugging easier, but now it is not really needed anymore. Good point 👍
13d4a75 to
9a2571e
Compare
Change added |
|
Hello @jayzhan211 , thank you for the review! I implemented the changes that you suggested. |
Which issue does this PR close?
Rationale for this change
Partial aggregate state schemas can contain duplicate field names when multiple order-sensitive aggregate expressions use the same ordering expression.
For example,
first_value(value ORDER BY timestamp)andlast_value(value ORDER BY timestamp)both exposed the ordering state field astimestamp@0.This required
DFSchema::try_fromto skipcheck_names()as a workaround. However, thestate_fields()contract requires state field names to be unique within the query.What changes are included in this PR?
AggregateUDFImpl::state_fields()implementation and toFirstValue/LastValue.DFSchema::check_names()inTryFrom<SchemaRef> for DFSchema.state_fields()implementation, including duplicate ordering field names within a single aggregate.For example, an ordering state field now has a name such as:
rather than the unqualified:
The ordering position also disambiguates repeated ordering fields within the same aggregate.
What is the testing strategy for this PR?
The change is covered by:
test_partial_aggregate_state_fields_have_unique_namestest_default_state_fields_namespaces_ordering_fieldsThe following test suites pass locally:
cargo fmt --all -- --checkandgit diff --checkalso pass.Are there any user-facing changes?
No public API signatures were changed. A new ordering_state_fields utility was added to centralize the naming of ordering state fields. Ordering state field names now use the aggregate namespace and ordering position to guarantee uniqueness, allowing DFSchema::check_names() to be re-enabled.